package net.godcode.olivenotes.entities;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;

@Entity
@Table(name = "Posts")
@NamedQueries({
	@NamedQuery(
		name = "Post.RECENT",
		query = "from Post order by created desc"
	),
	@NamedQuery(
		name = "Post.RECENT_PUBLISHED",
		query = "from Post as post where post.published=true order by created desc"
	),
	/*
	 * TODO: This doesn't work at the moment, so the DAO is using the criteria api.
	 */
	@NamedQuery(
		name = "Post.BY_TAG",
		query = "from Post as post inner join post.tags as tag with tag.name=:tagName order by created desc"
	)
})
/**
 * Post
 * 
 * @author Chris Lewis Dec 26, 2007 <chris@thegodcode.net>
 * @version $Id: Post.java 27 2008-01-20 13:13:20Z burningodzilla $
 */
public class Post extends BaseModel {
	
	/* Persistent fields. */
	
	@Column(nullable = false)
	private String title;
	
	@Column(columnDefinition = "text", nullable = false)
	private String body;
	
	@Column(nullable = false)
	private String name;
	
	@Column(nullable = false)
	private Date created;
	
	@Column(nullable = false)
	private Date modified;
	
	@Column(columnDefinition = "tinyint(1) default 0")
	private boolean published;
	
	/* Entity relationships. */
	
	@ManyToMany
	@JoinTable(
		name = "Posts_Tags", 
		joinColumns = { @JoinColumn(name = "post_id") }, 
		inverseJoinColumns = { @JoinColumn(name = "tag_id") } 
	)
	private Set<Tag> tags = new HashSet<Tag>();
	
	//TODO relate
	//@JoinColumn( name = "post_id" )
	//private Set<PostComment> comments = new HashSet<PostComment>();
	
	public Post() {}
	
	/**
	 * @return the body
	 */
	public String getBody() {
		return body;
	}

	/**
	 * @param body the body to set
	 */
	public void setBody(String body) {
		this.body = body;
	}

	/**
	 * @return the title
	 */
	public String getTitle() {
		return title;
	}

	/**
	 * @param title the title to set
	 */
	public void setTitle(String title) {
		this.title = title;
	}

	/**
	 * @return the created
	 */
	public Date getCreated() {
		return created;
	}

	/**
	 * @param created the created to set
	 */
	public void setCreated(Date created) {
		this.created = created;
	}

	/**
	 * @return the tags
	 */
	public Set<Tag> getTags() {
		return tags;
	}

	/**
	 * @return the published
	 */
	public boolean isPublished() {
		return published;
	}

	/**
	 * @param published the published to set
	 */
	public void setPublished(boolean published) {
		this.published = published;
	}
	
	public Set<PostComment> getComments() {
		//TODO
		return new HashSet<PostComment>();
	}

	/**
	 * @return the modified
	 */
	public Date getModified() {
		return modified;
	}

	/**
	 * @param modified the modified to set
	 */
	public void setModified(Date modified) {
		this.modified = modified;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}
	
}